package com.qidian.mes.machine.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.common.pojo.MESResult;
import com.qidian.mes.machine.mapper.MachineMaintainPlanMapper;
import com.qidian.mes.machine.service.MachineMaintainPlanService;
import com.qidian.mes.pojo.MaintainPlan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 任超
 * Time 2024/10/18 16:20
 */
@Service
public class MachineMaintainPlanServiceImpl implements MachineMaintainPlanService {

    @Autowired
    private MachineMaintainPlanMapper machineMaintainPlanMapper;

    @Override
    public MESResult selectList(MaintainPlan maintainPlan, int page, int limit) {
        PageHelper.startPage(page,limit);
        List<MaintainPlan> maintainPlans = machineMaintainPlanMapper.selectList(maintainPlan);
        PageInfo<MaintainPlan> pageInfo = new PageInfo<>(maintainPlans);
        return MESResult.success(pageInfo);
    }

    @Override
    public MESResult addPlan(MaintainPlan maintainPlan) {
        if (machineMaintainPlanMapper.selectByCode(maintainPlan.getPlanCode())!= null)
            return MESResult.error();
        if (machineMaintainPlanMapper.addPlan(maintainPlan) == 1)
            return MESResult.success(maintainPlan);
        return MESResult.error();
    }

    @Override
    public MESResult updatePlan(MaintainPlan maintainPlan) {
        MaintainPlan temp = machineMaintainPlanMapper.selectByCode(maintainPlan.getPlanCode());
        if (temp != null && !temp.getPlanCode().equals(maintainPlan.getPlanCode()))
            return MESResult.error();
        else
            machineMaintainPlanMapper.updatePlan(maintainPlan);
        return MESResult.success();
    }

    @Override
    public MESResult selectById(Long planId) {
        return MESResult.success(machineMaintainPlanMapper.selectById(planId));
    }

    @Override
    public MESResult updatePlanStatus(Long planId) {
        return MESResult.success(machineMaintainPlanMapper.updatePlanStatus(planId));
    }
}
